package com.terenac;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.R.bool;
import android.content.Context;
import android.widget.Toast;

public class PostService {
	
	public static boolean sendMessage(Message msg) {
		
		HttpStream request = new HttpStream();
		String httpAnswer = null;

		List<NameValuePair> userData = new ArrayList<NameValuePair>(2);
		userData.add(new BasicNameValuePair("action", "post"));
		userData.add(new BasicNameValuePair("sessionId", User.getSessionId() ));
		userData.add(new BasicNameValuePair("latitude", "" + msg.getLatitude()));
		userData.add(new BasicNameValuePair("longitude", "" + msg.getLongitude()));
		userData.add(new BasicNameValuePair("comment", msg.getDescription()));
		String apiUrl = "http://php.fer.hr/~mg44268/terenacapi.php";			
		

		try {
			//ovo treba otkomentirat kasnije
			httpAnswer = request.postMultipartPicture(apiUrl, userData, msg.getPicture());
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	public static String sendLoginRequest(String username, String pass) throws Exception{
		
		HttpStream request = new HttpStream();
		String httpAnswer = null;
		
		List<NameValuePair> userData = new ArrayList<NameValuePair>(2);
		userData.add(new BasicNameValuePair("action", "login"));
		userData.add(new BasicNameValuePair("username", username));
		userData.add(new BasicNameValuePair("password", pass));
		String apiUrl = "http://php.fer.hr/~mg44268/terenacapi.php";					

		try {
			httpAnswer = request.PostRequest(apiUrl, userData);
		} catch (Exception e) {
			throw new Exception("Problem with internet connection.");
		}
		
		JSONObject json_var;
		String id = null;
		
		try {
			json_var = new JSONObject(httpAnswer);
			id = json_var.getString("sessionId");
			return id;
		} catch (JSONException e1) {
			throw new Exception("Problem parsing answer.");
		}	 
		
		
	}
}
